package com.pengcheng.taskapplication.mvp.view.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.adapter.ParkAdapter;
import com.pengcheng.taskapplication.adapter.WashAdapter;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.helper.ImageLoader;
import com.pengcheng.taskapplication.model.ParkOrderModel;
import com.pengcheng.taskapplication.model.WashOrderModel;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenter;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenterV2;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.util.SPUtils;
import com.pengcheng.taskapplication.util.ToastUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ZFT on 2020/10/27.
 */

public class MyWashList extends BaseActivity implements IView,WashAdapter.OnStopLisener{
    private UserPresenterV2 userPresenter;
    @BindView(R.id.pull_to_refresh_lv)
    PullToRefreshListView pullToRefreshListView;

    @BindView(R.id.tv_center_title)
    TextView tv_center;

    private String token;



    ArrayList<WashOrderModel> list =new ArrayList<>();
    private WashAdapter adapter;

    private int page=0;
    private int pagesize=10;
    private String uid;
    private String newtoken;

    @Override
    protected int layoutResID() {
        return R.layout.activity_my_wash_list_layout;
    }

    @Override
    protected void initData() {

        uid= SPUtils.getString(context,ConstantValue.SpType.userid);
        newtoken=SPUtils.getString(context,ConstantValue.SpType.newtoken);

        tv_center.setText("订单列表");
        userPresenter = new UserPresenterV2(this);

        //adapter
        adapter = new WashAdapter(context,list);
        adapter.setOnStopLisner(this);
        //manager
        pullToRefreshListView.setAdapter(adapter);

        pullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                WashOrderModel model=(WashOrderModel) adapterView.getAdapter().getItem(i);
                Intent intent =new Intent(context,WashConfrimOrderActivity.class);
                intent.putExtra("from",1);
                intent.putExtra("tradno",model.getTradno());
                startActivity(intent);

            }
        });
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page=0;
                list.clear();
                queryWashOrderList(newtoken,uid,page,pagesize);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                queryWashOrderList(newtoken,uid,page,pagesize);
            }
        });

        queryWashOrderList(newtoken,uid,page,pagesize);
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if(actionType.equals(ConstantValue.RequestKey.queryWashOrderList)){
            Gson gson= new Gson();
            Type type =new TypeToken<ArrayList<WashOrderModel>>(){}.getType();
            ArrayList<WashOrderModel> orderModels= gson.fromJson(gson.toJson(data),type);
            list.addAll(orderModels);
            adapter.notifyDataSetChanged();
            pullToRefreshListView.onRefreshComplete();
        }
    }

    @Override
    public void onFailed(String code, String msg) {
        ToastUtils.showShortToast(msg);
    }

    @OnClick({R.id.img_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
        }
    }
    public void queryWashOrderList(String token,String uid,int page,int pagesize){
        userPresenter.queryWashOrderList(token,uid,page,pagesize,ConstantValue.RequestKey.queryWashOrderList);
    }

    @Override
    public void onStop(WashOrderModel washOrderModel) {
        //停止机器
    }
}
